import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/8 14:54
 * @description 广度优先遍历
 */
public class Solution {
    public static void main(String[] args) {

    }
    public int deepestLeavesSum(TreeNode root) {
        int ans = 0;
        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.add(root);
        while (!deque.isEmpty()) {
            int size = deque.size(), sum = 0;
            while (size > 0) {
                TreeNode temp = deque.poll();
                sum += temp.val;
                if (temp.left != null) {
                    deque.add(temp.left);
                }
                if (temp.right != null) {
                    deque.add(temp.right);
                }
                size--;
            }
            ans = sum;
        }
        return ans;
    }
}
